<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla controlleradmin library
jimport('joomla.application.component.controlleradmin');
 
/**
 * Guests Controller
 */
class MariagePlannerControllerTasks extends JControllerAdmin
{
	public function __construct($config = array())
	{
		parent::__construct($config);
		
		$this->text_prefix .= "_TASKS";
	}
	
	/**
	 * Proxy for getModel.
	 * @since	2.5
	 */
	public function getModel($name = 'Task', $prefix = 'MariagePlannerModel') 
	{
		$model = parent::getModel($name, $prefix, array('ignore_request' => false));
		return $model;
	}
	
	public function save()
	{
		// Check for request forgeries
		JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));
	
		// Get items to publish from the request.
		$cids = JRequest::getVar('cid', array(), '', 'array');
		$ids = JRequest::getVar('id', array(), '', 'array');
		
		$msgs = JRequest::getVar('message', array(), '', 'array');
		$dates = JRequest::getVar('date', array(), '', 'array');
		$states = JRequest::getVar('state', array(), '', 'array');
		$sms_states = JRequest::getVar('sms_state', array(), '', 'array');
		
		if (empty($cids))
		{
			JError::raiseWarning(500, JText::_($this->text_prefix . '_NO_ITEM_SELECTED'));
		}
		else
		{
			// Get the model.
			$model = $this->getModel();
	
			// Make sure the item ids are integers
			JArrayHelper::toInteger($cids);
			JArrayHelper::toInteger($ids);
			JArrayHelper::toInteger($states);
			JArrayHelper::toInteger($sms_states);
			
			$datas = array();
			foreach($cids as $cid) {
				$idx = array_search($cid, $ids);				
				$datas[] = array( 
						"id"=>$ids[$idx], 
						"message"=>$msgs[$idx], 
						"date"=>$dates[$idx], 
						"state"=>$states[$idx], 
						"sms_state"=>$sms_states[$idx]
				);
			}	
			
			// Save the items.
			foreach($datas as $data) {
				if (!$model->save($data))
				{
					JError::raiseWarning(500, $model->getError());
				}
			}
			
			$ntext = $this->text_prefix . '_N_ITEMS_SAVED';
			$this->setMessage(JText::plural($ntext, count($cids)));
		}
		$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false));
	}
}
?>